package com.clothing.dao.impl;

import javax.persistence.FlushModeType;
import javax.persistence.NoResultException;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;

import com.clothing.dao.UserDao;
import com.clothing.entity.User;

/**
 * @ClassName: UserDao
 * @Description: TODO(Dao - 用户)
 * @author Charles.Tao
 * @date 2016年3月24日 下午3:58:58
 * @version 1.0
 */
@Repository
public class UserDaoImpl extends BaseDaoImpl<User, Long> implements UserDao
{
    @Override
    public User findByMobile(String mobile)
    {
        String jpql = "from User user where user.mobile = :mobile";
        TypedQuery<User> query = entityManager.createQuery(jpql, User.class)
                .setFlushMode(FlushModeType.COMMIT)
                .setParameter("mobile", mobile);
        try
        {
            return query.getSingleResult();
        }
        catch(NoResultException e)
        {
            return null;
        }
    }

    @Override
    public User findByOpenid(String openid)
    {
        String jpql = "from User user where user.openid = :openid";
        TypedQuery<User> query = entityManager.createQuery(jpql, User.class).setFlushMode(FlushModeType.COMMIT)
                .setParameter("openid", openid);
        try
        {
            return query.getSingleResult();
        }
        catch(NoResultException e)
        {
            return null;
        }
    }
}
